package cn.xiaowenjie.controllers;

import cn.xiaowenjie.beans.AdvertQueryDto;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import javax.validation.Valid;

import cn.xiaowenjie.beans.Advert;
import cn.xiaowenjie.beans.PageObject;
import cn.xiaowenjie.beans.PicResult;
import cn.xiaowenjie.boss.form.AdvertForm;
import cn.xiaowenjie.common.beans.ResultBean;
import cn.xiaowenjie.response.PageResult;
import cn.xiaowenjie.services.AdvertService;
import io.swagger.annotations.Api;

/**
 * Created by Administrator on 2020/4/10.
 * <p>
 * by author wz
 * <p>
 * cn.xiaowenjie.controllers
 */
@Api(description = "广告api")
@RequestMapping("/advert")
@RestController
@CrossOrigin
public class AdvertController {

    @Autowired
    private AdvertService advertManagerService;

    @PostMapping("/all")
    public ResultBean<PageResult<Advert>> getAll(@RequestBody AdvertQueryDto pageObject) {
        return new ResultBean<PageResult<Advert>>(advertManagerService.getAll(pageObject.getPageNo(),
                pageObject.getPageSize(), pageObject.getAdId(), pageObject.getPosition()));
    }

    @PostMapping("/all/admin")
    public ResultBean<PageResult<Advert>> getAllAdmin(@RequestBody PageObject pageObject) {
        return new ResultBean<PageResult<Advert>>(advertManagerService.getAllAdmin(pageObject.getPageNo(), pageObject.getPageSize(), pageObject.getSchoolId()));
    }

    /**
     * 新增配置
     * <p>
     * FIXME 同时支持json格式和表单格式
     *
     * @return
     */
    @PostMapping("/add")
    public ResultBean<Long> add(@RequestBody @Valid AdvertForm advertForm) {
        return new ResultBean<Long>(advertManagerService.add(advertForm));
    }

    @PostMapping("/delete")
    public ResultBean<Boolean> delete(@RequestParam long id) {
        return new ResultBean<Boolean>(advertManagerService.delete(id));
    }

    /**
     * 上传图片功能
     *
     * @param file
     * @return
     */
    @PostMapping("image")
    public ResultBean<String> uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
        return new ResultBean<String>(this.advertManagerService.upload(file));
    }

    /**
     * 上传图片功能
     *
     * @param file
     * @return
     */
    @PostMapping("image/layedit")
    public String uploadImageedit(@RequestParam("imgFile") MultipartFile file) throws IOException {
        PicResult result = this.advertManagerService.uploadedit(file);
        JSONObject jb=new JSONObject();
        if(result!=null){
            jb.put("error", 0);
            jb.put("message", "上传成功！");
            jb.put("url",result.getSrc());
        }else {
            jb.put("url","");
            jb.put("error", 1);
        }

        return jb.toJSONString();
    }

    //    /**
//     * 上传图片功能
//     * @param file
//     * @return
//     */
//    @PostMapping("image/layedit")
//    public ResultBean<ImageDto> uploadImageForLayedit(@RequestParam("file") MultipartFile file) throws IOException {
//        ImageDto imageDto = new ImageDto();
//        imageDto.setSrc(this.advertManagerService.upload(file));
//        return new ResultBean<ImageDto>(imageDto);
//    }
    @PostMapping("/update")
    public ResultBean<Long> update(@RequestBody @Valid Advert advert) {
        return new ResultBean<Long>(advertManagerService.update(advert));
    }

}
